package com.createObject;

import java.io.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.Vector;

public class CreateObject {
    public static void main(String[] args) throws Exception{
        final Vector<Integer> integers = new Vector<>();

        //第一种 直接new 调用构造函数
        A obj1 = new A();
        obj1.hello();

        //第二种 clone
        //obj3 是obj2的克隆对象  没有调用构造函数
        B obj2 = new B();
        obj2.hello();
        B obj3 = (B) obj2.clone();
        obj3.hello();

        //第三种 序列化  没有调用构造函数
        C obj4  = new C();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data.obj"));
        out.writeObject(obj4);
        out.close();
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
        C obj5 = (C) in.readObject();
        in.close();
        obj5.hello();
        System.err.println(obj4+"|"+obj5);

        //第四种  newInstance  调用构造函数
        Object obj6 = Class.forName("pers.li.reflection.$1.createobj.A").newInstance();
        Method m = Class.forName("pers.li.reflection.$1.createobj.A").getMethod("hello");
        m.invoke(obj6);
        A obj7 = (A) Class.forName("pers.li.reflection.$1.createobj.A").newInstance();
    }
}



class A {
    public void hello()
    {
        System.out.println("hello from A");
    }
}

class B implements Cloneable {
    public void hello()
    {
        System.out.println("hello from B");
    }

    protected Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}
class C implements Serializable {
    private static final long serialVersionUID = 1L;
    public void hello() {
        System.out.println("hello from C");
    }
}